local UIShareDraw       = UIBase.New(PanelNames.UIShareDraw)

local m_result          = nil
local m_heroitem_map    = {}
local qualityToBgName   = { "hero_card_face_gre", "hero_card_face_blue", "hero_card_face_vio", "hero_card_face_yel", "hero_card_face_red", "hero_card_face_0" };
function UIShareDraw.Init(result)
    m_result = result
    UIBase.Init(UIShareDraw)

    logicMgr.ShareManager.SetShareType(EnumConst.ShareType.shareDraw)
end

function UIShareDraw:OnCreate()
    -- utimer.delayCall(function()
    --     closeUI(PanelResNames.UIShareDraw)
    -- end, 5000)

    for i=1, 10 do
        m_heroitem_map[i]   = self:GetChild("herobg" .. i)
    end

    self.txt_bannerTips     = self:GetChild("bannerTips")
    self.playerHead         = self:GetChild("playerHead")
    self.txt_playerName     = self:GetChild("playerName")
    self.txt_playerID       = self:GetChild("playerID")
    self.head               = self.playerHead:GetChild("head")
    self.img_Icon           = self.head:GetChild("HeadIcon")
    self.img_Frame          = self.head:GetChild("HeadFrameIcon")
    self.ctr_sex            = self:GetController("Gender")
    self.txt_lbs            = self:GetChild("LBS")
    self.ctr_sex            = logicMgr.HeroManager.GetRoleInfo("sex")

    self.ctr_showQRcode = self:GetController("showRWM")
    self.ctr_showQRcode.selectedIndex = 0

    -- breakStone = UIBreakStone:new(self.quality);
    if self.ctr_showQRcode.selectedIndex == 0 then
        self.img_qrcode     = self:GetChild("PicShare")
        ShareManager.GetQrcodeUrl(function(url)
            utimer.delayCall(function()
                self.img_qrcode.texture = NTexture.New(Util.CreateQRCode(url))
            end, 500)
        end)
    end

    self:BindHeroItems()
end

function UIShareDraw:BindHeroItems()
   
    if m_result then
        local ssr_num,shenmo = 0,0
        for k,v in pairs(m_result) do
            local config = G.dataTable["t_Hero"][tostring(v.itemModelId)]
            if config then
                if config.f_HeroInitialStar >= 60 then
                    ssr_num = ssr_num + 1

                    if config.f_HeroRace == 1 or config.f_HeroRace == 2 then
                        shenmo = shenmo + 1
                    end
                end
            end
            self:updateCard(k, m_heroitem_map[k], v)
        end

        if ssr_num == 0 then
            self:SetTips(1)
        elseif ssr_num > 0 and ssr_num < 4 then
            if shenmo > 0 then
                self:SetTips(3)
            else
                self:SetTips(2)
            end
        elseif ssr_num >= 4 then
            self:SetTips(4)
        end
    end
    -- LoginSDK.GetInstance():ShareTextureToWXFriend("yanhuammo/share.jpg", "分享内容文本测试")
    
    self:BindHead()
    closeUI(PanelResNames.UIScreenEffect)
    utimer.delayCall(function()
        LuaHelper.GetGameManager():ShareTexture(function()
            showUI(PanelResNames.UIShare, 1)
            showUI(PanelResNames.UIScreenEffect)
        end)
    end, 700)
end

function UIShareDraw:updateCard(index, itemObj, itemData)
    local heroid = itemData.itemModelId or itemData.modelId;
    local heroconfig = G.dataTable["t_hero"][tostring(heroid)];

    local img_icon  = itemObj:GetChild("Icon")
    img_icon = img_icon:GetChild("icon")
    local qualityBg = itemObj:GetChild("qualityBg")
    img_icon.url = "ui://HalfBodyAtlas/"..logicMgr.HeroManager.GetHeroBIcon(heroid)--"ui://HalfBodyAtlas/" .. heroconfig.f_HeroBIcon
    local quality = tonumber(heroconfig.f_HeroInitialStar) / 10;
    if quality == 1 then
        qualityBg.url = "ui://ICONAtlas/" .. qualityToBgName[1];
    else
        if math.floor(quality / 2) + 1 >= 6 then
            qualityBg.url = "ui://ICONAtlas/" .. qualityToBgName[6];
        else
            qualityBg.url = "ui://ICONAtlas/" .. qualityToBgName[math.floor(quality / 2) + 1];
        end
    end
end

function UIShareDraw:BindHead()
    self.txt_playerID.text = string.format(getLanguage("SharePlayerID"), logicMgr.HeroManager.GetRoleInfo().playerId) 

    local myself = logicMgr.HeroManager.GetRoleInfo();
    if myself ~= nil then
        local _roleName = logicMgr.HeroManager.GetRoleName()
        local level = logicMgr.HeroManager.GetRoleLevel()
        
        self.txt_playerName.text = _roleName
        self.img_Icon.url = logicMgr.SkinManager.GetHeroIconBySkinId(logicMgr.HeroManager.GetRoleInfo("pic"))
        local itemid = logicMgr.HeroManager.GetRoleInfo("picFrame")--frameId or logicMgr.HeroManager.GetRoleInfo("picFrame")
        self.img_Frame.url = EnumConst.AtlasPath.ICONAtlas .. itemid;
        self.txt_lbs.text = logicMgr.LBSManager.GetLocationName()

        -- local frameID = decodeJsonStrToTabel(config[tostring(itemid)]["f_ItemFunction"])
        -- if frameID[2] ~= 0 then
        --     if (particles ~= nil) then
        --         destroyImmediate(particles);
        --         particles = nil
        --     end
        --     resMgr:LoadModelPrefab("Fx/EFF_Frame_" .. frameID[2], function(go)
        --         local drawWrapper = drawWrappers;
        --         if (drawWrapper == nil) then
        --             drawWrapper = GoWrapper.New();
        --             drawWrappers = drawWrapper
        --         end
        --         particles = newObject(go);
        --         particles.name = tostring(frameID[2]);
        --         particles:AddComponent(TCanvas);
        --         drawWrapper:SetWrapTarget(particles, false)
        --         particle.visible = true;
        --         particle:SetNativeObject(drawWrapper);
        --         Util.Lua_SetLocalPositionXYZ(particles.transform,0,0,0)
        --     end)
        -- end
    end
end

function UIShareDraw:OnEnable()
    
end

function UIShareDraw:SetTips(type)
    self.txt_bannerTips.text = getLanguage("ShareDrawTips" .. type)
    TAManager.TrackByDictionary("share_success", {language = "ShareDrawTips" .. type})
end

function UIShareDraw:OnDestroy()
end

return UIShareDraw